<?php
/*
 * 此文件用于验证短信服务API接口，供开发时参考
 * 执行验证前请确保文件为utf-8编码，并替换相应参数为您自己的信息，并取消相关调用的注释
 * 建议验证前先执行Test.php验证PHP环境
 *
 * 2017/11/30
 */
require_once dirname(__DIR__) . "/Aliyun/SignatureHelper.php";

class Aliyun_Sms{
    private $accessKeyId = 'LTAIgC7I7GmhGICX';
    private $accessKeySecret = 'uK4RnS2CoKVRgENmsQF2Tx0xrRHBZH';
    private $Phone = '';
    private $SignName = '入货宝';
    private $TemplateCode = "SMS_151773866";
    private $Code = "";
    private $Product = "";

    public function phone($phone){
        $this->Phone = $phone;
        return $this;
    }
    public function template($templateCode){
        $this->TemplateCode = $templateCode;
        return $this;
    }
    public function code($code){
        $this->Code = $code;
        return $this;
    }
    public function product($product){
        $this->Product = $product;
        return $this;
    }

    public function send() {
        $params = array ();
        // *** 需用户填写部分 ***
        // fixme 必填：是否启用https
        $security = false;

        $accessKeyId = $this->accessKeyId;
        $accessKeySecret = $this->accessKeySecret;
        $params["PhoneNumbers"] = $this->Phone;
        $params["SignName"] = $this->SignName;
        $params["TemplateCode"] = $this->TemplateCode;
        $params['TemplateParam'] = Array (
            "code"      => $this->Code,
            "product"   => $this->Product,
        );

        // fixme 可选: 设置发送短信流水号
        $params['OutId'] = "12345";
        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
        $params['SmsUpExtendCode'] = "1234567";
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();
        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            )),
            $security
        );
        return $content;
    }
}

/*
 *  (new Aliyun_Sms)->phone('13140119168')->code('1234')->send();
 *
 */